from application import app,db
import www
from flask_script import Manager
from flask_script import Server
from flask_migrate import Migrate,MigrateCommand
from apps.crm.models import (
    User,
    Order,
    OperationHistory,
    ShareHistory,
    UseNumber,
    Order2Product,
    Setting
)
from traceback import print_exc
from apps.front.models import (
    FrontUser,
    Article,
    Desc,
    ProductExamples,
    OursGoodness,
    Swiper,
    Product,
    PickleType
)
Migrate(app=app,db=db)
manage = Manager(app=app)
manage.add_command('db',MigrateCommand)
manage.add_command('runserver',Server)


@manage.option('-u','--username',dest='username')
@manage.option('-p','--password',dest='password')
def createsuperuser(username,password):
    # try:
        user = User(login_name=username, login_pwd=password)
        db.session.add(user)
        db.session.commit()
    # except Exception as e:

        # print(e)
        # print(e.args)
    # else:
        print('用户 %s 创建成功'%username)

@manage.command
def test():
    user = db.session.query(User).filter(User.login_name == 'long123123').first()
    print(user.share_history.all())

if __name__ == '__main__':
    manage.run()

